var gl_vue_vm_list_html = $('#vue_vm_list').html();
var pageParamStr = $.url().param('pageParam');
var pageParam = JSON.parse(pageParamStr);

$(function() {
	$('#vue_vm_list div').hide();
	
	if(pageParam.type=='month') {
		findMonthInventoryColl(pageParam.beHospitalId, pageParam.yearMonth);
	}else if(pageParam.type=='day') {
		findDayInventoryColl(pageParam.beHospitalId, pageParam.yearMonthDay);
	}else if(pageParam.type=='out' || pageParam.type=='buda') {
		findOutInventoryColl(pageParam.beHospitalId);
	}
});

function findMonthInventoryColl(beHospitalId, yearMonth) {
	if($('#vue_vm_list div').length==0) {return;}
	$('#vue_vm_list div').hide();
	
	find_basic_by_json(path+'/elderPayRecord/findMonthInventoryColl', {beHospitalId:beHospitalId, month:yearMonth}, function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				getTitle:function() {
					return yearMonth;
				},
				getTotalMoney:function(todo) {
					return this.getElderPayRecordTotalMoney(todo.payRecordFroms) + this.getElderArrearageTotalMoney(todo.arreaForms);
				},
				getTotalMoneyCN:function(todo) {
					return moneyToCN(this.getTotalMoney(todo));
				},
				getMoneyToCN:function(money){
                    return moneyToCN(money);
				},
				getAllTotalMoney:function(todos) {
					var allTotalMoney = 0;
					
					if(todos) {
						for(var i=0; i<todos.length; i++) {
							allTotalMoney += getTotalMoney(todos[i]);
						}
					}
					
					return allTotalMoney;
				},
				getElderPayRecordTotalMoney:function(forms) {
					var totalMoney = 0;
					
					if(forms) {
						for(var i=0; i<forms.length; i++) {
                            if(forms[i].payWay==3){//如果是vip卡扣费
                                continue;
                            }
							totalMoney += forms[i].totalPayMoney;
						}
					}
					return totalMoney;
				},
				getElderArrearageTotalMoney:function(forms) {
					var totalMoney = 0;
					
					if(forms) {
						for(var i=0; i<forms.length; i++) {
							totalMoney += forms[i].totalArreaMoney;
						}
					}
					
					return totalMoney;
				},
				getLoginedStaffName:function() {
					return getLoginedUser()? getLoginedUser().staffName: '';
				},
				
				dateDiff: function(sDate1, sDate2) {
					return DateDiff(sDate1, sDate2);
				},
				getNowDate: function() {
					return getNowFormatDate().substr(0,10);
				},
                formatDateStr(date,format){
					return formatDate(date,format);
				},
				getNowDiff: function(sDate1) {
					var nowDate = getNowFormatDate().substr(0,10);
					return DateDiff(nowDate, sDate1);
				}
			}
		});
		
		$('#vue_vm_list div').show();
		
		if(showPaperUseNum) {
			showPaperUseNumByRelation(beHospitalId+"-outHospital",pageParam.paperType);
		}
	});
}

function findDayInventoryColl(beHospitalId, yearMonthDay) {
	if($('#vue_vm_list div').length==0) {return;}
	$('#vue_vm_list div').hide();
	
	find_basic_by_json(path+'/elderPayRecord/findDayInventoryColl', {beHospitalId:beHospitalId, day:yearMonthDay}, function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				getTitle:function() {
					return yearMonthDay;
				},
				getTotalMoney:function(todo) {
					return this.getElderPayRecordTotalMoney(todo.payRecordFroms) + this.getElderArrearageTotalMoney(todo.arreaForms);
				},
				getTotalMoneyCN:function(todo) {
					return moneyToCN(this.getTotalMoney(todo));
				},
                getMoneyToCN:function(money){
                    return moneyToCN(money);
                },
				getAllTotalMoney:function(todos) {
					var allTotalMoney = 0;
					
					if(todos) {
						for(var i=0; i<todos.length; i++) {
							allTotalMoney += getTotalMoney(todos[i]);
						}
					}
					return allTotalMoney;
				},
				getElderPayRecordTotalMoney:function(forms) {
					var totalMoney = 0;
					
					if(forms) {
						for(var i=0; i<forms.length; i++) {
                            if(forms[i].payWay==3){//如果是vip卡扣费
                                continue;
                            }
							totalMoney += forms[i].totalPayMoney;
						}
					}
					
					return totalMoney;
				},
				getElderArrearageTotalMoney:function(forms) {
					var totalMoney = 0;
					
					if(forms) {
						for(var i=0; i<forms.length; i++) {
							totalMoney += forms[i].totalArreaMoney;
						}
					}
					
					return totalMoney;
				},
				getLoginedStaffName:function() {
					return getLoginedUser()? getLoginedUser().staffName: '';
				},
				
				dateDiff: function(sDate1, sDate2) {
					return DateDiff(sDate1, sDate2);
				},
				
				getNowDate: function() {
					return getNowFormatDate().substr(0,10);
				},
                formatDateStr(date,format){
                    return formatDate(date,format);
                },
				getNowDiff: function(sDate1) {
					var nowDate = getNowFormatDate().substr(0,10);
					return DateDiff(nowDate, sDate1);
				}
			}
		});
		
		$('#vue_vm_list div').show();
	});
}

function findOutInventoryColl(beHospitalId) {
	if($('#vue_vm_list div').length==0) {return;}
	$('#vue_vm_list div').hide();
	
	find_basic_by_json(path+'/elderPayRecord/findByBeHospitalId', {beHospitalId:beHospitalId}, function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
        response.data.returnMoney = pageParam.returnMoney;
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
                getPayRecordSummAndGroup:function(){
                    if(this.todos){
                        var  elderPayRecordForms = this.todos[0].elderPayRecordForms;
                        var summAndGroupArr = [];
                        for(var i=0;i<elderPayRecordForms.length;i++){
                        	if(elderPayRecordForms[i].payWay==3){
                        		continue;
							}
                            var arrItem = {
                                costDesc:elderPayRecordForms[i].payWay==3?elderPayRecordForms[i].costDesc+"(VIP)":elderPayRecordForms[i].costDesc,
                                payMoney:elderPayRecordForms[i].payMoney,
                            }
                            var isHas = false;
                            for(var j=0;j<summAndGroupArr.length;j++){
                                if(summAndGroupArr[j].costDesc==arrItem.costDesc){
                                    summAndGroupArr[j].payMoney = summAndGroupArr[j].payMoney?summAndGroupArr[j].payMoney+arrItem.payMoney:arrItem.payMoney;
                                    isHas = true;
                                    break;
                                }
                            }
                            if(!isHas){
                                summAndGroupArr.push(arrItem);
                            }
                        }
                        return summAndGroupArr;
                    }
                },
				getTotalMoney:function(todo) {
					return this.getElderPayRecordTotalMoney(todo.elderPayRecordForms)
						+ this.getElderArrearageTotalMoney(todo.elderArrearageForms);
				},
				getTotalMoneyCN:function(todo) {
					return moneyToCN(this.getTotalMoney(todo));
				},
                getMoneyToCN:function(money){
                    return moneyToCN(money);
                },
				getAllTotalMoney:function(todos) {
					var allTotalMoney = 0;
					
					if(todos) {
						for(var i=0; i<todos.length; i++) {
							allTotalMoney += getTotalMoney(todos[i]);
						}
					}
					
					return allTotalMoney;
				},
				getElderPayRecordTotalMoney:function(forms) {
					var totalMoney = 0;
					
					if(forms) {
						for(var i=0; i<forms.length; i++) {
                            if(forms[i].payWay==3){//如果是vip卡扣费
                                continue;
                            }
							totalMoney += forms[i].payMoney;
						}
					}
					
					return totalMoney;
				},
				getElderArrearageTotalMoney:function(forms) {
					var totalMoney = 0;
					
					if(forms) {
						for(var i=0; i<forms.length; i++) {
							totalMoney += forms[i].arrearageMoney;
						}
					}
					
					return totalMoney;
				},
				getLoginedStaffName:function() {
					return getLoginedUser()? getLoginedUser().staffName: '';
				},
				
				dateDiff: function(sDate1, sDate2) {
					return DateDiff(sDate1, sDate2);
				},
				
				getNowDate: function() {
					return getNowFormatDate().substr(0,10);
				},
                formatDateStr(date,format){
                    return formatDate(date,format);
                },
				getNowDiff: function(sDate1) {
					var nowDate = getNowFormatDate().substr(0,10);
					return DateDiff(nowDate, sDate1);
				}
			}
		});
		
		$('#vue_vm_list div').show();
		
		if(showPaperUseNum) {
			var paperType = pageParam.paperType;
            showPaperUseNumByRelation(beHospitalId+"-outHospital",paperType);
		}
	});
}
